class config:
    def __init__(self, file):
        # SOME STUFF WE NEED
        xml = []            # AN XML ARRAY FOR THE FILE WE WANT TO PARSE
        idblocks = []       # AN ARRAY FOR OUR BLOCKS LIKE <a id="b">
        tmp = {}            # AN TEMP DICT FOR THE KEY & VALUES IN THE ID BLOCK
        self.conf = {}      # OUR FINAL DICT FOR ALL CONFIGS
        # SOME VARIABLES
        blockend = ''
        blockname = ''
        block = ''
        file = 'conf/' + file
        self.file = file
        # NOW COPY THE SPECIFIC FILE INTO THE XML ARRAY
        f = open(file, 'r')
        while True:
            line = f.readline()
            if line != '': xml.append(line)
            else: break
        f.close
        # NOW PARSE ARRAY xml AND CREATE A NEW DICT
        for x in range(0, len(xml)):
            idblock = False
            keyline = False
            noadd = False
            # DETECT AN ID BLOCK LIKE:
            # <block id="name">
            idstart = xml[x].find(' id=')
            if idstart != -1:
                idblock = True
                linebegin = xml[x].find('<')
                lineend = xml[x].find('>')
                block = xml[x][linebegin+1:idstart]
                blockname = xml[x][idstart+5:lineend-1]
                blockend = '</' + block + '>'
                idblocks.append(blockend)
                noadd = True
            # DETECT AN BLOCK ENDING LIKE:
            # </block>
            for i in range(0, len(idblocks)):
                if xml[x].find(idblocks[i]) != -1:
                    # NOW COPY THE TMP DICT TO THE CONF DICT
                    # AND CLOSE THE IDBLOCK
                    idblock = True
                    self.conf[blockname] = tmp
                    blockend = ''
                    blockname = ''
                    block = ''
                    tmp = {}
                    idblocks.remove(idblocks[i])
                    noadd = True
            # IF AN IDBLOCK WASN'T DETECTED
            # DETECT A SIMPLE LINE LIKE:
            # <key>value</key>
            if idblocks != [] and noadd == False:
                # IF AN IDBLOCK IS OPEN, COPY KEY & VALUE TO TEMP DICT
                keystart = xml[x].find("<")
                keyend = xml[x].find(">", keystart + 1)
                key = xml[x][keystart + 1:keyend]
                valueend = xml[x].find("<", keyend + 1)
                value = xml[x][keyend + 1:valueend]
                if key != '' and key != self.file[5:-4] and key != "/" + self.file[5:-4]:
                    tmp[key] = value
                key = ''
            if idblocks == [] and noadd == False:
                # IF NO IDBLOCK IS OPEN, COPY KEY AND VALUE TO CONF DICT
                keystart = xml[x].find("<")
                keyend = xml[x].find(">", keystart + 1)
                key = xml[x][keystart + 1:keyend]
                valueend = xml[x].find("<", keyend + 1)
                value = xml[x][keyend + 1:valueend]
                if key != '' and key != self.file[5:-4] and key != "/" + self.file[5:-4]:
                    self.conf[key] = value
                key = ''

    def write(self, key, value):
        f = file(self.file, 'w')
        f.write("<" + self.file[5:-4] + ">\n")
        write = False
        for i in range(0, len(self.conf)):
            confkey = self.conf.keys()[i]
            confvalue = self.conf[confkey]
            if confkey == key:
                f.write("  <" + confkey + ">" + value + "</" + confkey + ">\n")
                write = True
            else:
                f.write("  <" + confkey + ">" + confvalue + "</" + confkey + ">\n")
        # ADD VALUE IF IT DOES NOT EXIST
        if write == False:
            f.write("  <" + key + ">" + value + "</" + key + ">\n")
        f.write("</" + self.file[5:-4] + ">\n")
        f.close()
